<?php
class AvatarsController extends AppController {

	public $name = 'Avatars';
	public $components = array('Upload');
	
	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('index', 'cropImage', 'crop');
		//set style for curent user
		$this->setLayout();
	}
	
	
	public function index(){
		if(!empty( $this->data)){
			$path = WWW_ROOT.'files'.DS.'avatars';
			$this->Upload->upload($this->data['Avatar']['avatar']);
			if ($this->Upload->file_is_image){
				if($this->Upload->file_max_size > AVATAR_MAX_ZISE){
					// avatar original
	
			        $this->Upload->file_new_name_body = $this->Bayon->userId().'_original_'.date('Ymdhis');
					$this->Upload->process( $path );
					
					
					
					//initial avatar information for save
					if($this->Upload->processed){
						$info = getimagesize($this->Upload->file_dst_pathname);
						
						$this->Upload-> Clean();
						$this->redirect(array('action' => 'cropImage', $info[0], $info[1], $this->Upload->file_dst_name ));
						//$this->saveInfo($info, $this->Upload->file_dst_name, 1);
					}
					
        			
				
				}else{
					$this->Session->setFlash('Your avatar size is very big', true);
				}
			}else{
				$this->Session->setFlash('This file type not allow!', true);
			}	
		}else{
			$this->set('new_avatar', true);
			$this->set('avatars', 
				$this->Avatar->find(
					'all', 
					array(
						'conditions' => array(
							'Avatar.user_id' => $this->Bayon->userId()
						),
						'recursive' => -1
					)
				)
			);
		}
		
	}
	
	private function saveInfo($mime, $width, $name, $original = 0){
		$data = array(
			'Avatar' =>array(
			'user_id' => $this->Bayon->userId(),
			'original' => $original,
			'width' => $width,
			'height' =>$width, //width and height equal but with avatar 160px not equal it auto
			'mediatype' => $mime,
			'filename' => $name,
			'url' => FULL_BASE_URL.$this->base.'/files/avatars/'.$name,
			'created' => date('Y-m-d h:i:s')
		));
						
		//Start save original to database
		$this->Avatar->create();
		$this->Avatar->save($data);
	}
	
	public function cropImage($image_h, $image_w, $name){
		
		$orig_w = 300;
		
		$targ_w = 120;
		$targ_h = 100;
		
		$ratio = $targ_w / $targ_h;
		
		$info = array(
			'path' => FULL_BASE_URL.$this->base.'/files/avatars/'.$name,
			'targ_w' => $targ_w, 
			'targ_h' => $targ_h, 
			'ratio' => $ratio,
			'orig_w_h' => $orig_w,
			'name' => $name
		);
		$this->set('info', $info);
	}
	
	public function crop(){
		if(!empty($this->data)){
			//delete current avatar
			$this->__deleteCurrentAvatar($this->Bayon->userId());

			$path = WWW_ROOT.'files'.DS.'avatars';
			$this->Upload->upload($path.DS.$this->data['Avatar']['image_name']);
			$info = getimagesize($path.DS.$this->data['Avatar']['image_name']);
			
			//save original image
			$this->saveInfo($info['mime'], $info[0], $this->data['Avatar']['image_name'], 1);
			
		
			//Avatar 300x300
			$right_crop =  $info[0] - (AVATAR_WIDHT_300 + $this->data['Avatar']['x']);
			$bottom_crop = $info[1] - (AVATAR_WIDHT_300 + $this->data['Avatar']['y']); //height the same widht
			$this->Upload->image_crop = array($this->data['Avatar']['y'], $right_crop, $bottom_crop, $this->data['Avatar']['x']);
			$this->Upload->file_new_name_body = $this->Bayon->userId().'_300_'.date('Ymdhis');

			$this->Upload->process( $path );
			if ($this->Upload->processed){
				$this->saveInfo($info['mime'], AVATAR_WIDHT_300, $this->Upload->file_dst_name, 0);
			}
			
			//read image 300x300 just upload
			//$image_path = $path.DS.$this->Upload->file_dst_name.'<br/>';
			$this->Upload->upload($path.DS.$this->Upload->file_dst_name);
			
			//$info = getimagesize($path.DS.$this->Upload->file_dst_name);
			//echo $path.DS.$this->Upload->file_dst_name;
                        //Avatar 96x96
			$this->Upload->image_resize      = true;
			$this->Upload->image_y = AVATAR_WIDHT_96;
			$this->Upload->image_x = AVATAR_WIDHT_96;
			$this->Upload->file_new_name_body = $this->Bayon->userId().'_96_'.date('Ymdhis');
			$this->Upload->process( $path );
			if ($this->Upload->processed){
				$this->saveInfo($info['mime'], AVATAR_WIDHT_96, $this->Upload->file_dst_name);
			}
			
			//Avatar 96x96
			$this->Upload->image_resize      = true;
			$this->Upload->image_ratio_y = true;
			$this->Upload->image_x = AVATAR_WIDHT_160;
			$this->Upload->file_new_name_body = $this->Bayon->userId().'_160_'.date('Ymdhis');
			$this->Upload->process( $path );
			if ($this->Upload->processed){
				$this->saveInfo($info['mime'], AVATAR_WIDHT_96, $this->Upload->file_dst_name);
			}
			
			//Avatar 48x48
			$this->Upload->image_resize      = true;
			$this->Upload->image_y = AVATAR_WIDHT_48;
			$this->Upload->image_x = AVATAR_WIDHT_48;
			$this->Upload->file_new_name_body = $this->Bayon->userId().'_48_'.date('Ymdhis');
			$this->Upload->process( $path );
			if ($this->Upload->processed){
				$this->saveInfo($info['mime'], AVATAR_WIDHT_48, $this->Upload->file_dst_name);
			}
			
			//Avatar 24x24
			$this->Upload->image_resize      = true;
			$this->Upload->image_y = AVATAR_WIDHT_24;
			$this->Upload->image_x = AVATAR_WIDHT_24;
			$this->Upload->file_new_name_body = $this->Bayon->userId().'_24_'.date('Ymdhis');
			$this->Upload->process( $path );
			if ($this->Upload->processed){
				$this->saveInfo($info['mime'], AVATAR_WIDHT_24, $this->Upload->file_dst_name);
			}
					
			
		}//end if empty
	}

/**
 * Delete current avatar
 * 
 */
	private function __deleteCurrentAvatar($userId){

		$avatars = $this->Avatar->getAllAvatars($userId);
		if(!empty($avatars)){
			foreach( $avatars as $avatar){
				$path = WWW_ROOT.'files'.DS.'avatars'.DS.$avatar['Avatar']['filename'];
				$this->__deleteAvatar($path);
				
			}
			
			//clean table avatar for this current user
			$this->Avatar->deleteAll(array('Avatar.user_id' => $userId));
		}
	}
	
	private function __deleteAvatar($path){
		App::import('File');
		$file = new File($path);
		$file->delete();
	}
	
}
?>